package com.atguigu.gmall.common.config.executor;

import com.atguigu.gmall.common.config.executor.properties.ThreadPoolProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author:lyd
 **/
@Configuration
//1、开启配置文件和属性自动绑定 2、将ThreadPoolProperties注入到容器中
@EnableConfigurationProperties(ThreadPoolProperties.class)
@Slf4j
public class ThreadPoolExecutorConfiguration {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolProperties properties) {
        log.info("创建线程池");
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                properties.getCorePoolSize(),
                properties.getMaxPoolSize(),
                properties.getKeepAliveTime(),
                properties.getUnit(),
                new LinkedBlockingQueue<>(properties.getWorkQueueSize()),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        return threadPoolExecutor;
    }
}
